package com.zhongzhi.serviceimpl;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongzhi.dao.AisleDao;
import com.zhongzhi.dao.EquipmentDao;
import com.zhongzhi.dao.MeasureDao;
import com.zhongzhi.service.AisleService;
import com.zhongzhi.springboot.pojo.Aisle;
import com.zhongzhi.springboot.pojo.Aisle_E;
import com.zhongzhi.springboot.pojo.Bo;
import com.zhongzhi.springboot.pojo.Type;

@Service
public class AisleServiceImpl implements AisleService{

	@Autowired
	private AisleDao aisleDao;
	@Autowired
	private MeasureDao measureDao;

	//新增
	@Override
	public Bo insert(Aisle aisle) {
		//参数校验

		if(aisle.getAisleName()==null || aisle.getAisleName().length()==0) {
			return new Bo(false,"通道名称为空",0,null);
		}else if (aisle.getEquipmentId()==null || aisle.getEquipmentId()<=0) {
			return new Bo(false,"设备为空或设备选择错误",0,null);
		}else if (aisle.getNumber()==null || aisle.getNumber()<=0 || aisle.getNumber() > 32) {
			return new Bo(false,"通道号为空",0,null);
		}else {
			//查询同一设备的通道号是否重复
			int a =aisleDao.selectNumberEquipmentId(aisle);
			if(a!=0) {
				return new Bo(false,"通道号重复",0,null);
			}
			int b = aisleDao.insert(aisle);
			if (b==0) {
				return new Bo(false,"插入数据失败",0,null);
			}
			return new Bo(false,"",b,null);

		}

	}

	//修改
	@Override
	public Bo update(Aisle aisle) {
		//参数校验
		if(aisle.getAisleName()==null || aisle.getAisleName().length()==0) {
			return new Bo(false,"通道名称为空",0,null);
		}else if (aisle.getEquipmentId()==null || aisle.getEquipmentId()<=0) {
			return new Bo(false,"设备为空或设备选择错误",0,null);
		}else if (aisle.getNumber()==null || aisle.getNumber()<=0 || aisle.getNumber() > 32) {
			return new Bo(false,"通道号为空",0,null);
		}else {
			//查询同一设备的通道号是否重复
			int a =aisleDao.selectNumberEquipmentId(aisle);
			if(a!=0) {
				return new Bo(false,"通道号重复",0,null);
			} 
			int b = aisleDao.update(aisle);
			if (b==0) {
				return new Bo(false,"修改数据失败",0,null);
			}
			return new Bo(true,"",b,null);

		}
	}

	//删除
	@Override
	public Bo delete(Integer id) {
		if(id<=0) {
			return new Bo(false,"通道选择有误",0,null);
		}
		//根据通道id查询数据,判断这个通道有无数据
		int a = measureDao.selectByaisleId(id);
		if(a>0) {
			return new Bo(false,"通道有测量数据,无法删除通道",0,null);
		}else {
			int b = aisleDao.delete(id);
			if(b==0) {
				return new Bo(false,"删除通道数据失败",0,null);
			}
			return new Bo(true,"",b,null);
		}
	}
	//分页查全部
	@Override
	public Bo select(Integer pages, Integer line) {
		//数据校验
		//页数
		if(pages==null || pages<0) {
			pages = 0;
		}
		//条数
		if(line ==null || line<=0) {
			line=20;
		}
		pages=pages*line;
		List<Aisle_E> list = aisleDao.select(pages,line);
		return new Bo(true, "",list.size(),list);
	}
	//查询总数
	@Override
	public Bo selectSize() {
		int a = aisleDao.selectSize();
		return new Bo(true, "",a,null);
	}

	@Override
	public Bo selectById(Integer id) {
		//数据校验
		if (id<=0) {
			return new Bo(false, "通道id错误",0,null);
		}else {
			Aisle_E aisle = aisleDao.selectById(id);
			if (aisle==null) {
				return new Bo(false, "没此id数据",0,null);
			}else {
				return new Bo(true, "",10,aisle);
			}
		}
	}


}
